<?php

class ssitemap extends CI_Model {

	function __construct() {
		parent:: __construct();
		$this->load->database();
		$this->load->helper('file');
		$this->load->helper('myurl_helper');
		$this->load->helper('url');
	}
	function get_link_cate($cat){
		$linkTo = category_url($cat->cat_code);
		return $linkTo;
	}
	function get_link($game){ 
		$linkTo = game_url($game->game_id,$game->game_code);
		return $linkTo;
	}

	function get_link_post($post){
		$linkTo = post_url($post->pos_id,$post->pos_code);
		return $linkTo;
	}
	/*
	if $file_old_delete ==1 then delete file
	
	*/
	function buid_gzip($arr_link, $file_old_delete = 0){
		$arr_new = array();
		if(count($arr_link)>0){
			foreach( $arr_link as $r){
			// Name of the file we are compressing
			$file = $r;

			// Name of the gz file we are creating
			$gzfile = $r.".gz";
			if(file_exists($gzfile))
				unlink($gzfile);
			$arr_new[] = $gzfile;
			// Open the gz file (w9 is the highest compression)
			$fp = gzopen ($gzfile, 'w9');

			// Compress the file
			gzwrite ($fp, file_get_contents($file));

			// Close the gz file and we are done
			gzclose($fp);
			if($file_old_delete ==1)
				unlink($file);
			}
		}
		return $arr_new;
	}
	
	function build_index($arr_link){
		$data = '<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			$data.= "\n";
			$file_name = 'sitemap/index'.'.xml';
			if(file_exists($file_name))
				unlink($file_name);
			write_file('./'.$file_name, $data,'a+');
			$mytime = date("Y-m-d\TH:i:s")."+00:00";
			$mytime = date("Y-m-d");
			foreach($arr_link as $r){
				 $data= "<sitemap>\n";
				 $data.= "\t<loc>".base_url().$r."</loc>\n";
				 $data.= "\t<lastmod>".$mytime."</lastmod>\n";
				 $data.= "</sitemap>\n";
				 write_file('./'.$file_name, $data,'a+');
			}
			$data = "</sitemapindex>\n";
			write_file('./'.$file_name, $data,'a+');
			echo "write file: ".$file_name."<br>";
		return (array)$file_name;
	}

	public function post_detail(){
		$ci = & get_instance();
		$arr_file = array();
		$limit = 5000;
		$this->db->from("post");
		$this->db->where('pos_status >', 0)
				->where('pos_date_start <', time());
		$total_row = $this->db->count_all_results();
		$total = (int)($total_row/$limit)+1;
		$dem = 0;
		for($i=0; $i<$total; $i++){
			$this->db->from("post");
			$this->db->where('pos_status >', 0)
				->where('pos_date_start <', time());
			$this->db->limit($limit,$i*$limit);
			$datas = $this->db->get()->result();
			$data = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">';
			$data.= "\n";
			$file_name = 'sitemap/sitemap-post-detail-'.$i.'.xml';
			if(file_exists($file_name))
				unlink($file_name);
			write_file('./'.$file_name, $data,'a+');
			$arr_file[] = $file_name;
			foreach($datas as $r){
				
				$data= "<url>\n";
				$full_link = $this->get_link_post($r);
				$priority = '0.5';
				$changefreq = 'monthly';
				//2014-03-26T00:01:00+07:00
				$lastmod = date("Y-m-d",$r->pos_date_create)."T".date("H:i:s",$r->pos_date_update).'+00:00';
				$lastmod = date("Y-m-d",$r->pos_date_update);
				$thumb = image_thumbnail($r->pos_image, 'large');
				$data.= "\t<loc>".$full_link."</loc>\n";
				
				$data.= "\t<image:image>
				<image:loc>".$thumb."</image:loc>
				</image:image>\n";
				
				$data.= "\t<priority>".$priority."</priority>\n";
				$data.= "\t<lastmod>".$lastmod."</lastmod>\n";
				$data.= "\t<changefreq>".$changefreq."</changefreq>\n";
				$data.= "</url>\n";
				write_file('./'.$file_name, $data,'a+');
			}
			$data = "</urlset>\n";
			write_file('./'.$file_name, $data,'a+');
			echo "write file: ".$file_name."<br>";
		}
		return $arr_file;
	}

	public function game_detail(){
		$arr_file = array();
		$limit = 5000;
		$this->db->from("game");
		$this->db->where('game_status >', 0);
		$total_row = $this->db->count_all_results();
		$total = (int)($total_row/$limit)+1;
		$dem = 0;
		for($i=0; $i<$total; $i++){
			$this->db->from("game");
			$this->db->where('game_status >', 0);
			$this->db->limit($limit,$i*$limit);
			$datas = $this->db->get()->result();
			$data = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			$data.= "\n";
			$file_name = 'sitemap/sitemap-game-detail-'.$i.'.xml';
			if(file_exists($file_name))
				unlink($file_name);
			write_file('./'.$file_name, $data,'a+');
			$arr_file[] = $file_name;
			foreach($datas as $r){
				$data= "<url>\n";
				$full_link = $this->get_link($r);
				$priority = rand(70,70)/100;
				//2014-03-26T00:01:00+07:00
				$lastmod = date("Y-m-d",$r->game_time_update)."T".date("H:i:s",$r->game_time_update).'+00:00';
				$lastmod = date("Y-m-d",$r->game_time_update);
				$changefreq = 'daily';
				$thumb = image_thumbnail($r->game_thumb, 'large');
				$data.= "\t<loc>".$full_link."</loc>\n";
				/*
				$data.= "\t<image:image>
				<image:loc>".$thumb."</image:loc>
				</image:image>\n";
				*/
				$data.= "\t<priority>".$priority."</priority>\n";
				$data.= "\t<lastmod>".$lastmod."</lastmod>\n";
				$data.= "\t<changefreq>".$changefreq."</changefreq>\n";
				$data.= "</url>\n";
				write_file('./'.$file_name, $data,'a+');
			}
			$data = "</urlset>\n";
			write_file('./'.$file_name, $data,'a+');
			echo "write file: ".$file_name."<br>";
		}
		return $arr_file;
	}
	public function category(){
		$ci = & get_instance();
		$arr_file = array();
		$limit = 5000;
		$this->db->from("category");
		$this->db->where('cat_status >', 0);
		$total_row = $this->db->count_all_results();
		$total = (int)($total_row/$limit)+1;
		$dem = 0;
		for($i=0; $i<$total; $i++){
			$this->db->from("category");
			$this->db->where('cat_status >', 0);
			$this->db->limit($limit,$i*$limit);
			$datas = $this->db->get()->result();
			$data = '<?xml version="1.0" encoding="UTF-8"?>
		<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			$data.= "\n";
			$file_name = 'sitemap/sitemap-category-'.$i.'.xml';
			if(file_exists($file_name))
				unlink($file_name);
			write_file('./'.$file_name, $data,'a+');
			$arr_file[] = $file_name;
			foreach($datas as $r){
				$data= "<url>\n";
				$full_link = $this->get_link_cate($r);
				$priority = '0.1';
				$changefreq = 'yearly';
				$lastmod = date("Y-m-d",$r->cat_time_update)."T".date("H:i:s",$r->cat_time_update).'+00:00';
				$lastmod = date("Y-m-d",$r->cat_time_update);
				$data.= "\t<loc>".$full_link."</loc>\n";
				$data.= "\t<priority>".$priority."</priority>\n";
				$data.= "\t<lastmod>".$lastmod."</lastmod>\n";
				$data.= "\t<changefreq>".$changefreq."</changefreq>\n";
				$data.= "</url>\n";
				write_file('./'.$file_name, $data,'a+');
			}
			$data = "</urlset>\n";
			write_file('./'.$file_name, $data,'a+');
			echo "write file: ".$file_name."<br>";
		}
		return $arr_file;
	}
	
	public function series(){
		$arr_file = array();
		$limit = 5000;
		$this->db->from("series");
		$this->db->where('series_status >', 0);
		$total_row = $this->db->count_all_results();
		$total = (int)($total_row/$limit)+1;
		$dem = 0;
		for($i=0; $i<$total; $i++){
			$this->db->from("series");
			$this->db->where('series_status >', 0);
			$this->db->limit($limit,$i*$limit);
			$datas = $this->db->get()->result();
			$data = '<?xml version="1.0" encoding="UTF-8"?>
		<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
			$data.= "\n";
			$file_name = 'sitemap/sitemap-series-'.$i.'.xml';
			if(file_exists($file_name))
				unlink($file_name);
			write_file('./'.$file_name, $data,'a+');
			$arr_file[] = $file_name;
			foreach($datas as $r){
				$data= "<url>\n";
				$full_link = series_url($r->series_code);
				$priority = rand(30,30)/100;
				$lastmod = date("Y-m-d",$r->series_time_update)."T".date("H:i:s",$r->series_time_update).'+00:00';
				$changefreq = 'always';
				$data.= "\t<loc>".$full_link."</loc>\n";
				$data.= "\t<image:image>
					<image:loc>".image_thumbnail($r->series_thumb, 'large')."</image:loc>
				</image:image>\n";
				$data.= "\t<priority>".$priority."</priority>\n";
				$data.= "\t<lastmod>".$lastmod."</lastmod>\n";
				$data.= "\t<changefreq>".$changefreq."</changefreq>\n";
				$data.= "</url>\n";
				write_file('./'.$file_name, $data,'a+');
			}
			$data = "</urlset>\n";
			write_file('./'.$file_name, $data,'a+');
			echo "write file: ".$file_name."<br>";
		}
		return $arr_file;
	}
}
?>
	
	